<?php
// define variables and set to empty values
$nameErr = $emailErr = $countryCodeErr = $telErr = $languagefromErr = $languagetoErr =  "";
$storyErr = $authorshipErr = $legalpermissionErr = "";
$name = $email = $countryCode = $tel = $languagefrom = $languageto = $story = "";
$authorship = $legalpermission = "No";
$originalid = "";
$originalidErr = "";
$wordsentered = 0;
$originaltitle = "";
$originallanguagename = "";
$translatedlanguagename = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name/pseudonym is required";
  } else {
    $name = test_input($_POST["name"]);
    $nameErr = "";
  }

  if (empty($_POST["legalpermission"])){
    $legalpermissionErr = "Please indicate if you are an adult in your region of residence or if not, have permission from your legal guardian/parent(s) to make a submission";
  }else{
    $legalpermission = test_input($_POST["legalpermission"]);
    $legalpermissionErr = "";
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
          $email = test_input($_POST["email"]);
          // check if e-mail address is well-formed
          if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                  $emailErr = "Invalid email format";
          } else {
                  $emailErr = "";
          }
  }

  $countryCode = test_input($_POST["countryCode"]);

  if (empty($_POST["tel"])) {
    $telErr = "Telephone is required";
  } else {
    $tel = test_input($_POST["tel"]);
    $telErr = "";
  }

  if (empty($_POST["languagefrom"])) {
    $languagefromErr = "Please choose one of the available language options to translate from";
  } else {
    $languagefrom = test_input($_POST["languagefrom"]);
    $languagefromErr = "";
  }

  if (empty($_POST["languageto"])) {
    $languagetoErr = "Please choose one of the available language options to translate to";
  } else {
    $languageto = test_input($_POST["languageto"]);
    $languagetoErr = "";
  }

  if (empty($_POST["title"])) {
    $titleErr = "Please add the translated story title";
  } else {
    $title = test_input($_POST["title"]);
    $titleErr = "";
  }

  if (empty($_POST["originalid"])) {
    $originalidErr = "Please ensure you have selected a story to translate";
  } else {
    $originalid = test_input($_POST["originalid"]);
    $originalidErr = "";
  }

  if (empty($_POST["translator"])) {
    $translatorErr = "Please confirm that this is your translation and that you allow others to use it provided that they attribute you as the translator.";
  } else {
    $translator = test_input($_POST["translator"]);
    $translatorErr = "";
  }

  if (empty($_POST["story"])) {
    $storyErr = "Please entry your translated story";
  } else {
    $story = test_input($_POST["story"]);
    $wordsentered = wordcount($story);
    if (($wordsentered < 1400 ) || ($wordsentered > 3100 )) {
        $storyErr = "Your translated story needs to be between 1500 and 3000 words, and is currently ". $wordsentered . " words.";
    } else {
      $storyErr = "";
    }
  }

}
 
  function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
  }

  function wordcount($story) {
          $words = explode(' ', $story);
          $wordcount = count($words);
          return $wordcount;
  }
?>
